<?php

class bbsController extends base
{

    public function __construct()
    {
        parent::__construct();
    }
    /**
     * 社区主页面数据
    */
    public function dobbsHome()
    {
        $uid = $this->uid;
        $focusPicture = $this->load('bbs')->getFocusPic();
        $boards = $this->load('bbs')->getBoardList(false);
        $dailyShow = $this->load('bbs')->getDailyShow();
        $hotPostIds = $this->load('bbs')->getTopHotPost();
        $hotPost = array();
        if(!empty($hotPostIds))
        {
            foreach ($hotPostIds as $postId)
            {
                $postInfo = $_ENV['bbs']->getPostInfo($postId);
                if(!empty($postInfo) && $postInfo['is_show'])
                {
                    $hotPost[] = array('news_id'    => $postInfo['news_id'],
                                      'title'       => $postInfo['title'],
                                      'short_desc'  => $postInfo['short_desc'],
                                      'thumb_img'   => $postInfo['thumb_img'],
                                      'uid'         => $postInfo['uid'],
                                      'authorHeader'  => $this->redis->hget(REDIS_PREFIX.'UserInfo_'.$postInfo['uid'],'header'),
                                      'add_time'    => $postInfo['add_time'],
                                      'loves'       => $postInfo['loves'],
                                      'total_comment'  => $postInfo['total_comment']
                        );
                }
            }
        }
        $this->result['result'] = compact('focusPicture','boards','dailyShow','hotPost');
    }
    /**
     * 社区主页面(2.5.0+)
    */
    public function dohome(){
        if($this->afterVersion('2.7.9', '2.7.9')){
            $this->_home28();
        }else{
            $this->_home25();
        }
    }
    
    public function _home28(){
        $uid = $this->uid;
        $updated_at = common::request('updated_at','G',0);
        $redisKey = REDIS_PREFIX.'BbsHome28';
        
        if($this->redis->exists($redisKey)){
            $result = json_decode($this->redis->GET($redisKey),true);
            $result['now'] = time();
            //实时闪购信息
            $flashInfo = null;//$this->load('goods')->getHomeFlashInfo();
            $result['flashInfo'] = $flashInfo;
            //随机微课信息
            $liveInfo = $this->load('live/live')->getHomeLiveInfo($uid);
            $result['liveInfo'] = $liveInfo;
            if($updated_at && $updated_at >= $result['updated_at']){
                $result = (object)[];
            }
        }else{
            //banner
            $bbs_mod = $this->load('bbs');
            $focusPicture = $bbs_mod->getFocusPic();
            
            //导航
            $channels = $bbs_mod->getBbsChannel();
            foreach($channels as $k=>$row){
                if(strpos($row['scheme'],'joggers://')===false){
                    continue;
                }
                $row['scheme'] = strpos($row['scheme'],'?') ? $row['scheme'].'&vn='.$this->vn : $row['scheme'].'?vn='.$this->vn;
            }
            //$channels[1]['scheme'] = $channels[1]['scheme'].'?vn='.$this->vn;
            
            //热门话题
            $_topics = $this->load('topic')->getFeaturedTopics();
            $topics = [];
            foreach($_topics as $key=>$topic){
                if($topic['home']){
                    $topics[] = $topic;
                    array_splice($_topics,$key,1);
                }
            }
            $count = count($topics);
            if($count<3){
                for($i=0;$i<3-$count;$i++){
                    $topics[] = $_topics[$i];
                }
            }
            
            //跑友秀
            $showtime = $bbs_mod->getPosition(false);
            
            //装备咨询
            $gearNewsBanner = $bbs_mod->getGearNewsBanner();
            $gearNews = [];
            $news_ids = $bbs_mod->getGearNews(3);
            foreach($news_ids as $news_id){
                $post = $bbs_mod->getPostInfo($news_id);
                $post['content'] = strip_tags($post['content']);
                $post['content'] = str_replace(["\r","\t","\n"],'',$post['content']);
                $gearNews[] = $post;
            }
            //每日问答
            $qa = [];
            $qa_ids = $bbs_mod->getRandAns(5);
            foreach($qa_ids as $news_id){
                $post = $bbs_mod->getPostInfo($news_id);
                $post['content'] = strip_tags($post['content']);
                $post['content'] = str_replace(["\r","\t","\n"],'',$post['content']);
                $qa[] = $post;
            }
            
            //活动
            $activity = $this->load('activity')->activityList(0,'next',2,true);
            
            $updated_at = time();
            $activityScheme = 'http://irun.hupu.com/show/activityList';

            //闪购信息
            $flashInfo = null;//$this->load('goods')->getHomeFlashInfo();
            //微课信息
            $liveInfo = $this->load('live/live')->getHomeLiveInfo($uid);
            $result = compact('focusPicture','channels','topics','showtime','gearNewsBanner','gearNews','qa','activityScheme','activity','updated_at','flashInfo', 'liveInfo');


            $this->redis->SET($redisKey,json_encode($result));
            $this->redis->EXPIRE($redisKey,60);
            $result['now'] = time();
        }

        $this->result['result'] = $result;
    }
    
    public function _home25()
    {
    
        $focusPicture = $this->load('bbs')->getFocusPic();

        // 热门帖子
        $this->load('bbs')->updateHomeBestPost();
        $hotPostIds = $_ENV['bbs']->getHomeBestPostList();
        $hotPost = array();
        if(!empty($hotPostIds))
        {
            $uid = $this->uid;
            $this->load('user');
            $this->load('client');

            foreach ($hotPostIds as $postId)
            {
                $postInfo = $_ENV['bbs']->getPostInfo($postId);
                if(!empty($postInfo) && $postInfo['is_show'])
                {
                    $userInfo = $_ENV['client']->getUserInfo($postInfo['uid']);
                    $tmpPost = array('news_id'     => $postInfo['news_id'],
                                      'type'       => isset($postInfo['type']) ? $postInfo['type'] : $_ENV['bbs']->getPostType($postInfo['content']),
                                      'title'       => $postInfo['title'],
                                      'short_desc'  => $postInfo['short_desc'],
                                      'tag'  => $postInfo['tag'],
                                      'thumb_img'   => $postInfo['thumb_img'],
                                      'middle_img'  => $_ENV['bbs']->getPostMidImg($postInfo['sourceImg']),
                                      'source_img'  => $postInfo['sourceImg'],
                                      'uid'         => $postInfo['uid'],
                                      'nickname'  => isset($userInfo['nickname']) ? $userInfo['nickname'] : '',
                                      'header'  => isset($userInfo['header']) ? $userInfo['header'] : '',
                                      'attendance'  => $_ENV['user']->getAttendance($postInfo['uid']),
                                      'isLove' => $this->redis->ZSCORE(REDIS_PREFIX.'PostLikeList_'.$postId, $uid) === false ? 0 : 1,
                                      'isCollect' => $this->redis->ZSCORE(REDIS_PREFIX.'CollectPostList_'.$uid, $postId) === false ? 0 : 1,
                                      'add_time'    => $postInfo['add_time'],
                                      'loves'       => $postInfo['loves'],
                                      'total_comment'  => $postInfo['total_comment'],
                                      'imginfo'  => isset($postInfo['imginfo']) ? json_decode($postInfo['imginfo'],true) : array()
                        );
                    // 图文混排类型的帖子，图片只显示一张
                    if($tmpPost['type'] == 1)
                    {
                       $tmpPost['thumb_img'] = !empty($tmpPost['thumb_img']) ? array($tmpPost['thumb_img'][0]) : array();
                       $tmpPost['middle_img'] = !empty($tmpPost['middle_img']) ? array($tmpPost['middle_img'][0]) : array();
                       $tmpPost['source_img'] = !empty($tmpPost['source_img']) ? array($tmpPost['source_img'][0]) : array();                           $tmpPost['imginfo'] = !empty($tmpPost['imginfo']) ? array($tmpPost['imginfo'][0]) : array();
                    }
                    // 列表中帖子显示的文字内容
                    $tmpPost['content'] = $tmpPost['type'] ? $postInfo['short_desc'] : $postInfo['content'];
                    $tmpPost['content'] = trim($tmpPost['content']);
                    !$tmpPost['content'] && $tmpPost['content'] = $tmpPost['title'];

                    // 喜欢帖子的用户列表
                    $loveList = $_ENV['bbs']->getPostLikeList($postId,0,'next',8);
                    if($loveList)
                    {
                        foreach ($loveList as $key => $v)
                        {
                            unset($loveList[$key]['level'],$loveList[$key]['nickname'],$loveList[$key]['gender'],$loveList[$key]['total_rate'],$loveList[$key]['total_mileage']);
                        }
                    }
                    $tmpPost['loveList'] = empty($loveList) ? array() : $loveList;
                    // 评论列表
                    $tmpPost['comments'] = $_ENV['bbs']->getCommentsNoImg($postId);

                    $hotPost[] = $tmpPost;
                }
            }
        }

        if($this->afterVersion('2.6.0', '2.6.0'))
        {
            // 频道列表
            $channels = array(
                array(
                    'title' => 'ShowTime',
                    'bg_img' => 'http://' . $this->config['host'] . '/static/images/youdao_showtime.png',
                    'scheme' => 'joggers://bbs/showtime',
                ),
                array(
                    'title' => '问答',
                    'bg_img' => 'http://' . $this->config['host'] . '/static/images/youdao_wenda.png',
                    'scheme' => 'joggers://bbs/qa',
                ),
                array(
                    'title' => '装备', 
                    'bg_img' => 'http://' . $this->config['host'] . '/static/images/youdao_gear.png',
                    'scheme' => 'joggers://bbs/gear',
                ),
                array(
                    'title' => '活动',
                    'bg_img' => 'http://' . $this->config['host'] . '/static/images/youdao_huodong.png',
                    'scheme' => 'http://irun.hupu.com/show/activityList',
                ),
            );
        }
        else
        {
            // 频道列表
            $channels = array(
                array(
                    'title' => '问答',
                    'bg_img' => 'http://' . $this->config['host'] . '/static/images/bbs_channel_question.png',
                    'scheme' => 'joggers://bbs/qa',
                ),
                array(
                    'title' => '活动',
                    'bg_img' => 'http://' . $this->config['host'] . '/static/images/bbs_channel_huodong.png',
                    'scheme' => 'http://irun.hupu.com/show/activityList',
                ),
                array(
                    'title' => 'Showtime',
                    'bg_img' => 'http://' . $this->config['host'] . '/static/images/bbs_channel_showtime.png',
                    'scheme' => 'joggers://bbs/showtime',
                ),
                array(
                    'title' => '装备', 
                    'bg_img' => 'http://' . $this->config['host'] . '/static/images/bbs_channel_zhuangbei.png',
                    'scheme' => 'joggers://bbs/gear',
                ),
            );
        }

        $showtime = $this->load('bbs')->getPosition(false);
        $this->result['result'] = compact('focusPicture', 'channels','hotPost','showtime');
    }
    /**
     * 社区首页推荐帖子列表(2.5.0+)
    */
    public function dohomeBestPosts()
    {
        // 获取参数
        $news_id = common::request('news_id', 'G',0);
        $direc = common::request('direc', 'G', 'next');
        $offset = common::request('offset', 'G', '10');
        $boards = array();

        // 推荐帖子
        $this->load('bbs');
        $hotPostIds = $_ENV['bbs']->getHomeBestPostList($news_id, $direc, $offset);
        $hotPost = array();
        if(!empty($hotPostIds))
        {
            $this->load('user');
            $this->load('client');
            $uid = $this->uid;
            foreach ($hotPostIds as $postId)
            {
                $postInfo = $_ENV['bbs']->getPostInfo($postId);
                if(!empty($postInfo) && $postInfo['is_show'])
                {
                    $userInfo = $_ENV['client']->getUserInfo($postInfo['uid']);
                    if($postId == 149)
                    {
                      print_r($postInfo);exit;
                    }
                    $tmpPost = array('news_id'     => $postInfo['news_id'],
                                      'type'       => isset($postInfo['type']) ? $postInfo['type'] : $_ENV['bbs']->getPostType($postInfo['content']),
                                      'title'       => $postInfo['title'],
                                      'short_desc'  => $postInfo['short_desc'],
                                      'tag'  => $postInfo['tag'],
                                      'thumb_img'   => $postInfo['thumb_img'],
                                      'middle_img'   => $_ENV['bbs']->getPostMidImg($postInfo['sourceImg']),
                                      'source_img'   => $postInfo['sourceImg'],
                                      'uid'         => $postInfo['uid'],
                                      'nickname'  => isset($userInfo['nickname']) ? $userInfo['nickname'] : '',
                                      'header'  => isset($userInfo['header']) ? $userInfo['header'] : '',
                                      'attendance'  => $_ENV['user']->getAttendance($postInfo['uid']),
                                      'isLove' => $this->redis->ZSCORE(REDIS_PREFIX.'PostLikeList_'.$postId, $uid) === false ? 0 : 1,
                                      'isCollect' => $this->redis->ZSCORE(REDIS_PREFIX.'CollectPostList_'.$uid, $postId) === false ? 0 : 1,
                                      'add_time'    => $postInfo['add_time'],
                                      'loves'       => $postInfo['loves'],
                                      'total_comment'  => $postInfo['total_comment'],
                                      'imginfo'  =>  isset($postInfo['imginfo']) ? json_decode($postInfo['imginfo'],true) : array()
                        );
                    // 图文混排类型的帖子，图片只显示一张
                    if($tmpPost['type'] == 1)
                    {
                       $tmpPost['thumb_img'] = !empty($tmpPost['thumb_img']) ? array($tmpPost['thumb_img'][0]) : array();
                       $tmpPost['middle_img'] = !empty($tmpPost['middle_img']) ? array($tmpPost['middle_img'][0]) : array();
                       $tmpPost['source_img'] = !empty($tmpPost['source_img']) ? array($tmpPost['source_img'][0]) : array();                           $tmpPost['imginfo'] = !empty($tmpPost['imginfo']) ? array($tmpPost['imginfo'][0]) : array();
                    }
                    // 列表中帖子显示的文字内容
                    $tmpPost['content'] = $tmpPost['type'] ? $postInfo['short_desc'] : $postInfo['content'];
                    $tmpPost['content'] = trim($tmpPost['content']);
                    !$tmpPost['content'] && $tmpPost['content'] = $tmpPost['title'];
                    
                    // 喜欢帖子的用户列表
                    $loveList = $_ENV['bbs']->getPostLikeList($postId,0,'next',8);
                    if($loveList)
                    {
                        foreach ($loveList as $key => $v)
                        {
                            unset($loveList[$key]['level'],$loveList[$key]['nickname'],$loveList[$key]['gender'],$loveList[$key]['total_rate'],$loveList[$key]['total_mileage']);
                        }
                    }
                    $tmpPost['loveList'] = empty($loveList) ? array() : $loveList;
                    // 评论列表
                    $tmpPost['comments'] = $_ENV['bbs']->getCommentsNoImg($postId);

                    $hotPost[] = $tmpPost;
                }
            }
        }

        $this->result['result'] = compact('focusPicture','boards','hotPost');
    }

    /**
     * showtime页面信息(2.5.0+)
    */
    public function doshowtime()
    {
        // 获取参数
        $boardId = common::request('boardId', 'G', 'showtime');
        $postListType = common::request('postListType', 'G','0');
        $news_id = common::request('news_id', 'G',0);
        $direc = common::request('direc', 'G', 'next');
        $offset = common::request('offset', 'G', '10');
        $topic = common::request('topic','G');
        $news_uid = common::request('news_uid','G');

        if(empty($boardId))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        
        if($postListType==4 && !$topic){
            $_ENV['error']->getErrors(101);
            exit;
        }
        
        if($postListType==5 && (!$news_uid || !is_numeric($news_uid))){
            $_ENV['error']->getErrors(101);
            exit;
        }

        // 版块帖子列表
        if($postListType == 4 && $topic){
            $topicInfo = $this->load('topic')->getTopicInfo($topic);
            if($topicInfo && $topicInfo['status'] == 1 ){
                $postIds = null;
            }elseif($topicInfo){
                //$topic = str_replace('#','刱',$topic);
                //$postIds = $this->load('bbs')->search('"'.$topic.'"',$boardId,$news_id,$direc,$offset+10);
                $postIds = $this->load('topic')->getPostIdFromTopic($topicInfo['topic_id'],$news_id,$offset+10);
                if($topicInfo['feature_posts']){
                    $postIds = array_diff($postIds,$topicInfo['feature_posts']);
                    if(!$news_id){
                        $postIds = array_merge($topicInfo['feature_posts'],$postIds);
                    }
                }
            }
        }elseif($postListType == 5 && $news_uid){
            $postIds = $this->load('bbs')->getUserPostList($boardId, $news_uid, $news_id, $direc, $offset+10);
        }else{
            $postIds = $this->load('bbs')->getBoardPostList($boardId, $postListType, $news_id, $direc, $offset+10);
        }
        $postList = array();
        if(!empty($postIds))
        {
            $uid = $this->uid;
            $this->load('user');
            $this->load('client');
            foreach ($postIds as $postId)
            {
                if(count($postList) >= 10)
                {
                    break;
                }

                $postInfo = $this->load('bbs')->getPostInfo($postId);
                if(!empty($postInfo) && $postInfo['is_show'])
                {
                    $userInfo = $_ENV['client']->getUserInfo($postInfo['uid']);

                    $tmpPost = array('news_id'     => $postInfo['news_id'],
                                      'type'       => isset($postInfo['type']) ? $postInfo['type'] : $_ENV['bbs']->getPostType($postInfo['content']),
                                      'title'       => $postInfo['title'],
                                      'short_desc'  => $postInfo['short_desc'],
                                      'tag'  => $postInfo['tag'],
                                      'thumb_img'   => $postInfo['thumb_img'],
                                      'middle_img'   => $_ENV['bbs']->getPostMidImg($postInfo['sourceImg']),
                                      'source_img'   => $postInfo['sourceImg'],
                                      'uid'         => $postInfo['uid'],
                                      'nickname'  => isset($userInfo['nickname']) ? $userInfo['nickname'] : '',
                                      'header'  => isset($userInfo['header']) ? $userInfo['header'] : '',
                                      'attendance'  => $_ENV['user']->getAttendance($postInfo['uid']),
                                      'isLove' => $this->redis->ZSCORE(REDIS_PREFIX.'PostLikeList_'.$postId, $uid) === false ? 0 : 1,
                                      'isCollect' => $this->redis->ZSCORE(REDIS_PREFIX.'CollectPostList_'.$uid, $postId) === false ? 0 : 1,
                                      'add_time'    => $postInfo['add_time'],
                                      'loves'       => $postInfo['loves'],
                                      'total_comment'  => $postInfo['total_comment'],
                                      'imginfo'  =>  isset($postInfo['imginfo']) ? json_decode($postInfo['imginfo'],true) : array(),
                        );

                    if($this->vn >= 2820){
                        $tmpPost['energy'] = $postInfo['energy'];
                        $tmpPost['energy_pay_count'] = $postInfo['energy_pay_count'];
                        $tmpPost['is_paid_energy'] = $_ENV['client']->isPostPaidBy($postInfo['news_id'],$this->uid);
                    }
                    // 图文混排类型的帖子，图片只显示一张
                    if($tmpPost['type'] == 1)
                    {
                       $tmpPost['thumb_img'] = !empty($tmpPost['thumb_img']) ? array($tmpPost['thumb_img'][0]) : array();
                       $tmpPost['middle_img'] = !empty($tmpPost['middle_img']) ? array($tmpPost['middle_img'][0]) : array();
                       $tmpPost['source_img'] = !empty($tmpPost['source_img']) ? array($tmpPost['source_img'][0]) : array();
                       $tmpPost['imginfo'] = !empty($tmpPost['imginfo']) ? array($tmpPost['imginfo'][0]) : array();
                    }
                    // 列表中帖子显示的文字内容
                    $tmpPost['content'] = $tmpPost['type'] ? $postInfo['short_desc'] : $postInfo['content'];
                    // 喜欢帖子的用户列表
                    $loveList = $_ENV['bbs']->getPostLikeList($postId,0,'next',8);
                    if($loveList)
                    {
                        foreach ($loveList as $key => $v)
                        {
                            unset($loveList[$key]['level'],$loveList[$key]['nickname'],$loveList[$key]['gender'],$loveList[$key]['total_rate'],$loveList[$key]['total_mileage']);
                        }
                    }
                    $tmpPost['loveList'] = empty($loveList) ? array() : $loveList;
                    // 评论列表
                    $tmpPost['comments'] = $_ENV['bbs']->getCommentsNoImg($postId);

                    $postList[] = $tmpPost;
                }
            }
        }

        $this->result['result'] = $postList;
    }
    /**
     * 问答版块信息(2.5.0+)
    */
    public function doqa()
    {
        // 获取参数
        $news_id = common::request('news_id', 'G',0);
        $direc = common::request('direc', 'G', 'next');
        $offset = common::request('offset', 'G', '10');

        // 版块附加模块
        $addition = $news_id > 0 ? array() : $this->load('bbs')->getBoardAdditions('hotans');

        // 版块帖子列表
        $postIds = $this->load('bbs')->getBoardPostList('hotans', 0, $news_id, $direc, $offset);
        //common::pr($postIds);exit;
        $postList = array();
        if(!empty($postIds))
        {
            foreach ($postIds as $postId)
            {
                $postInfo = $_ENV['bbs']->getPostInfo($postId);
                if(!empty($postInfo) && $postInfo['is_show'])
                {
                    $tmpPost = array('news_id'     => $postInfo['news_id'],
                                      'title'       => $postInfo['title'],
                                      'uid'         => $postInfo['uid'],
                                      'header'  => $this->redis->hget(REDIS_PREFIX.'UserInfo_'.$postInfo['uid'],'header'),
                                      'total_comment'  => $postInfo['total_comment']
                        );
                    // 评论列表
                    $default=$this->redis->hget(REDIS_PREFIX.'defaultAns',$news_id);
                    if($default)
                    {
                      $sql="select uid, comment_content from r_news_comment where comment_id = {$default} ";
                      $tmpPost['comments'] = $_ENV['bbs']=$this->db->getRow($sql);
                    }
                    else
                    {
                    $tmpPost['comments'] = $_ENV['bbs']->getCommentsNoImg($postId, 1);
                    }
                    $postList[] = $tmpPost;
                }
            }
        }

        $this->result['result'] = compact('addition', 'postList');
    }
    /**
     * 问答搜索推荐帖子
    */
    public function dosearchRecPost_old()
    {
        $keywords = common::request('keywords', 'G');

        $keywords = trim($keywords);

        if(!$keywords)
        {
            $_ENV['error']->getErrors(101);
            exit;
        }

        $postIds = $this->load('bbs')->search($keywords, 'hotans');

        $keywords = strtolower($keywords);
        $strSearch = $keywords != strtoupper($keywords) ? array($keywords, strtoupper($keywords)) : $keywords;
        $strReplace = is_array($strSearch) ? array('<b>'.$keywords.'</b>', '<b>'.strtoupper($keywords).'</b>') : '<b>'.$keywords.'</b>';
        $postList = array();
        foreach ($postIds as $v)
        {
            $postInfo = $_ENV['bbs']->getPostInfo($v);
            if(!empty($postInfo) && $postInfo['is_show'])
            {
                $postList[] = array('news_id'     => $postInfo['news_id'],
                                    'title'       => str_replace($strSearch, $strReplace, $postInfo['title'])
                              );
            }
        }

        $this->result['result'] = $postList;
    }
    
    public function dosearchRecPost(){
        $keywords = common::request('keywords', 'G');
        $keywords = trim($keywords);
        if(!$keywords){
            $_ENV['error']->getErrors(101);
            exit;
        }
        
        $postList = $this->load('bbs')->searchRec($keywords, 'hotans');
        
        foreach($postList as &$v){
            $v['title'] = str_replace(['<em>','</em>'],['<b>','</b>'],$v['title']);
        }
        
        $this->result['result'] = $postList;
    }
    
    /**
     * 问答搜索帖子列表
    */
    public function dosearchPost()
    {
        $keywords = common::request('keywords', 'G');
        $news_id = common::request('news_id', 'G', 0);
        $direc = common::request('direc', 'G', 'next');
        $offset = common::request('offset', 'G', 10);

        $keywords = trim($keywords);
        if(!$keywords)
        {
            $_ENV['error']->getErrors(101);
            exit;
        }

        $postIds = $this->load('bbs')->search($keywords, 'hotans', $news_id, $direc, $offset);

        $postList = array();
        foreach ($postIds as $v)
        {
            $postInfo = $_ENV['bbs']->getPostInfo($v);
            if(!empty($postInfo) && $postInfo['is_show'])
            {
                $tmpPost = array('news_id'     => $postInfo['news_id'],
                                  'title'       => $postInfo['title'],
                                  'uid'         => $postInfo['uid'],
                                  'header'  => $this->redis->hget(REDIS_PREFIX.'UserInfo_'.$postInfo['uid'],'header'),
                                  'total_comment'  => $postInfo['total_comment']
                    );
                // 评论列表
                $tmpPost['comments'] = $_ENV['bbs']->getCommentsNoImg($v, 1);

                $postList[] = $tmpPost;
            }
        }

        $this->result['result'] = $postList;
    }

    /**
     * 帖子热门评论
    */
    public function dohotComments()
    {
        // 获取参数
        $news_id = common::request('news_id', 'G',0);
        $news_id = intval($news_id);

        //统计 点击数据
        $this->load('news')->makeNewsClickStat($news_id);
        
        if(empty($news_id))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }

        $postInfo = $this->load('bbs')->getPostInfo($news_id);
        $hotComments = array();
        if(!empty($postInfo) && $postInfo['is_show'] != 0)
        {
            $hotComments = $this->load('bbs')->getHotComment($news_id);
        }

        $this->result['result'] = $hotComments;
    }

    /**
     * 版块页面信息
    */
    public function doboardData()
    {
        $boardId = common::request("boardId",  'G');
        if(empty($boardId))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $boardInfo = $this->load('bbs')->getBoardInfo($boardId);
        unset($boardInfo['sortOrder']);

        $postIds = $this->load('bbs')->getBoardPostList($boardId);
        $postList = array();
        if($postIds)
        {
            foreach ($postIds as $postId)
            {
                $postInfo = $_ENV['bbs']->getPostInfo($postId);
                if(!empty($postInfo) && $postInfo['is_show'])
                {
                    $postList[] = array('news_id'     => $postInfo['news_id'],
                                      'title'       => $postInfo['title'],
                                      'short_desc'  => $postInfo['short_desc'],
                                      'thumb_img'   => $postInfo['thumb_img'],
                                      'uid'         => $postInfo['uid'],
                                      'authorHeader'  => $this->redis->hget(REDIS_PREFIX.'UserInfo_'.$postInfo['uid'],'header'),
                                      'add_time'    => $postInfo['add_time'],
                                      'loves'       => $postInfo['loves'],
                                      'total_comment'  => $postInfo['total_comment']
                        );
                }
            }
        }
        $this->result['result'] = compact('boardInfo', 'postList');
    }

    /**
     * 版块帖子列表
    */
    public function doboardPostList()
    {
        $boardId = common::request('boardId', 'G');
        $postListType = common::request('postListType', 'G','0');
        $news_id = common::request('news_id', 'G',0);
        $direc = common::request('direc', 'G', 'next');
        $offset = common::request('offset', 'G', '10');
        if(empty($boardId))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $postIds = $this->load('bbs')->getBoardPostList($boardId, $postListType, $news_id, $direc, $offset);
        $postList = array();
        if($postIds)
        {
            foreach ($postIds as $postId)
            {
                $postInfo = $_ENV['bbs']->getPostInfo($postId);
                if(!empty($postInfo) && $postInfo['is_show'])
                {
                    $postList[] = array('news_id'     => $postInfo['news_id'],
                                      'title'       => $postInfo['title'],
                                      'short_desc'  => $postInfo['short_desc'],
                                      'thumb_img'   => $postInfo['thumb_img'],
                                      'uid'         => $postInfo['uid'],
                                      'authorHeader'  => $this->redis->hget(REDIS_PREFIX.'UserInfo_'.$postInfo['uid'],'header'),
                                      'add_time'    => $postInfo['add_time'],
                                      'loves'       => $postInfo['loves'],
                                      'total_comment'  => $postInfo['total_comment']
                        );
                }
            }
        }
        $this->result['result'] = compact('postListType', 'postList');
    }
    /**
     * 获取帖子信息及热门评论列表
    */
    public function dopostData()
    {
        $news_id = common::request('news_id', 'G');
        $gid = common::request('gid', 'G');
        $news_id = intval($news_id);
        $can_pay_energy = 0;

        //统计 点击数据
        $this->load('news')->makeNewsClickStat($news_id);
        
        if(empty($news_id))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $bbs_mod = $this->load('bbs');
        $postInfo = $bbs_mod->getPostInfo($news_id);

        if(!empty($postInfo) && $postInfo['is_show'] > 0)
        {
            $is_top = $this->load('group')->getTopByNewsId($news_id,$gid);
            $block = array();
            if(!empty($is_top)){
                $block = array(array(
                    'title'=>'置顶',
                    'color'=>'#FF0000'
                ));
            }
            $postInfo['block']=$block;
            if($this->afterVersion('2.7.9', '2.7.9')){
                $postInfo['content'] = $bbs_mod->buildPostTopic($postInfo['content']);
            }
            if(isset($postInfo['type']) && $postInfo['type'] == 0 && $this->vn < 2500)
            {
                $postInfo['content'] = $bbs_mod->buildPostContent($postInfo['content'], $postInfo['sourceImg']);
            }
            $postInfo['content'] = $bbs_mod->buildPostAt($postInfo['content']);
            $postInfo['content'] = $bbs_mod->buildWidget($postInfo['content'],$news_id,$this->afterVersion('2.9.1','2.9.1') ? 1 : 2);
            
            $postInfo['imginfo'] =isset($postInfo['imginfo']) ? json_decode($postInfo['imginfo'],true) : array();

            $isShowTime = 0;
            foreach ($postInfo['board'] as $v)
            {
                if($v['boardId'] == 'showtime')
                {
                  $isShowTime = 1;
                  break;
                }
            }
            if($postInfo['type'] == 0 && $postInfo['title'] && $this->vn >= 2500)
            {
                if($isShowTime)
                {
                    $postInfo['content'] = $postInfo['content'];
                    $postInfo['title'] = '';
                }
            }
            if($isShowTime){
                $can_pay_energy = 1;
            }

            unset($postInfo['board'],$postInfo['author'],$postInfo['real_addtime'],$postInfo['start_time'],$postInfo['is_show'],
                $postInfo['imgCount'],$postInfo['a_start_time'],$postInfo['is_top'],$postInfo['lastupdate'],$postInfo['a_end_time'],$postInfo['a_channel']);
            $postInfo['middle_img'] = $_ENV['bbs']->getPostMidImg($postInfo['sourceImg']);
            $postInfo['friendstatus'] = $this->load('friends')->getFriendStatus($this->uid, $postInfo['uid']);
        }
        else
        {
            $_ENV['error']->getErrors(801);
            exit;
        }
        $bannerImg = '';
        $actImgs = $this->load('activity')->getBeginAct();
        foreach($actImgs as $k=>$info){
            if(strpos($info['link'],"$news_id") !== false){
                $bannerImg = $info['img'];
            }
        }
        $isLove = $this->redis->ZSCORE(REDIS_PREFIX.'PostLikeList_'.$news_id, $this->uid) === false ? 0 : 1;
        $isCollect = $this->redis->ZSCORE(REDIS_PREFIX.'CollectPostList_'.$this->uid, $news_id) === false ? 0 : 1;
        $loveList = $this->load('bbs')->getPostLikeList($news_id,0,'next',8);
        if($loveList)
        {
            foreach ($loveList as $key => $v) {
                unset($loveList[$key]['level'],$loveList[$key]['nickname'],$loveList[$key]['gender'],$loveList[$key]['total_rate'],$loveList[$key]['total_mileage']);
            }
        }
        $hotComments = $this->load('bbs')->getHotComment($news_id);
        $is_paid_energy = $this->load('client')->isPostPaidBy($news_id,$this->uid);
        $rel = compact('postInfo','isLove','isCollect','loveList','bannerImg','hotComments','is_paid_energy','can_pay_energy');
        if($this->vn >= 2100){
            $newComments = $this->load('bbs')->getCommentList($news_id,0,'next',10);
            $rel['newComments'] = $newComments;
        }
        $this->result['result'] = $rel;
    }
    /**
     * 获取获取标签列表
    */
    public function dotagList()
    {
        $boardId = common::request('boardId','G');
        $boardId = trim($boardId);
        if(empty($boardId))
        {
            $board = $this->redis->ZRANGEBYSCORE(REDIS_PREFIX.'BoardList', '1', '+inf');
        }
        else
        {
            $board[] = $boardId;
        }

        $this->result['result'] = $this->load('bbs')->getTagList($board);
    }
    /**
     * 获取标签下帖子列表
    */
    public function dopostListByTag()
    {
        $tagId = common::request('tagId','G');
        $postListType = common::request('postListType','G', 0);
        $news_id = common::request('news_id','G', 0);
        $direc = common::request('direc','G','next');
        $offset = common::request('offset','G','10');
        if(empty($tagId)) {
            $_ENV['error']->getErrors(101);
            exit;
        }

        $tagInfo = $this->load('bbs')->getTagInfo($tagId);
        empty($tagInfo) && $tagInfo = (object)$tagInfo;
        $postIds = $this->load('bbs')->getTagPostList($tagId,$postListType, $news_id, $direc, $offset);

        $postList = array();

        $this->load('user');
        $this->load('client');
        $uid = $this->uid;
        foreach ($postIds as $postId)
        {
          $postInfo = $_ENV['bbs']->getPostInfo($postId);
          if(!empty($postInfo) && $postInfo['is_show'])
          {
            $userInfo = $_ENV['client']->getUserInfo($postInfo['uid']);
            if($this->vn >= 2500)
            {
                  $tmpPost = array('news_id'     => $postInfo['news_id'],
                                    'type'       => isset($postInfo['type']) ? $postInfo['type'] : $_ENV['bbs']->getPostType($postInfo['content']),
                                    'title'       => $postInfo['title'],
                                    'short_desc'  => $postInfo['short_desc'],
                                    'tag'  => $postInfo['tag'],
                                    'thumb_img'   => $postInfo['thumb_img'],
                                    'middle_img'   => $_ENV['bbs']->getPostMidImg($postInfo['sourceImg']),
                                    'source_img'   => $postInfo['sourceImg'],
                                    'uid'         => $postInfo['uid'],
                                    'nickname'  => isset($userInfo['nickname']) ? $userInfo['nickname'] : '',
                                    'header'  => isset($userInfo['header']) ? $userInfo['header'] : '',
                                    'attendance'  => $_ENV['user']->getAttendance($postInfo['uid']),
                                    'isLove' => $this->redis->ZSCORE(REDIS_PREFIX.'PostLikeList_'.$postId, $uid) === false ? 0 : 1,
                                    'isCollect' => $this->redis->ZSCORE(REDIS_PREFIX.'CollectPostList_'.$uid, $postId) === false ? 0 : 1,
                                    'add_time'    => $postInfo['add_time'],
                                    'loves'       => $postInfo['loves'],
                                    'total_comment'  => $postInfo['total_comment'],
                                    'imginfo'  =>  isset($postInfo['imginfo']) ? json_decode($postInfo['imginfo'],true) : array()
                      );
                  // 图文混排类型的帖子，图片只显示一张
                    if($tmpPost['type'] == 1)
                    {
                       $tmpPost['thumb_img'] = !empty($tmpPost['thumb_img']) ? array($tmpPost['thumb_img'][0]) : array();
                       $tmpPost['middle_img'] = !empty($tmpPost['middle_img']) ? array($tmpPost['middle_img'][0]) : array();
                       $tmpPost['source_img'] = !empty($tmpPost['source_img']) ? array($tmpPost['source_img'][0]) : array();
                       $tmpPost['imginfo'] = !empty($tmpPost['imginfo']) ? array($tmpPost['imginfo'][0]) : array();
                    }
                  // 列表中帖子显示的文字内容
                  $tmpPost['content'] = $tmpPost['type'] ? $postInfo['short_desc'] : $postInfo['content'];
                  // 喜欢帖子的用户列表
                  $loveList = $_ENV['bbs']->getPostLikeList($postId,0,'next',8);
                  if($loveList)
                  {
                      foreach ($loveList as $key => $v)
                      {
                          unset($loveList[$key]['level'],$loveList[$key]['nickname'],$loveList[$key]['gender'],$loveList[$key]['total_rate'],$loveList[$key]['total_mileage']);
                      }
                  }
                  $tmpPost['loveList'] = empty($loveList) ? array() : $loveList;
                  // 评论列表
                  $tmpPost['comments'] = $_ENV['bbs']->getCommentsNoImg($postId);
            }
            else
            {
                $tmpPost = array('news_id'     => $postInfo['news_id'],
                                  'title'       => $postInfo['title'],
                                  'short_desc'  => $postInfo['short_desc'],
                                  'thumb_img'   => $postInfo['thumb_img'],
                                  'uid'         => $postInfo['uid'],
                                  'authorHeader'  => isset($userInfo['header']) ? $userInfo['header'] : '',
                                  'add_time'    => $postInfo['add_time'],
                                  'loves'       => $postInfo['loves'],
                                  'total_comment'  => $postInfo['total_comment']
                    );
            }

            $postList[] = $tmpPost;
          }
        }
        $this->result['result'] = compact('tagInfo', 'postList');
    }
    /**
     * 获取评论列表
    */
    public function docommentList()
    {
        $news_id = common::request('news_id','G', 0);
        $comment_id = common::request('comment_id','G', 0);
        $direc = common::request('direc','G','next');
        $offset = common::request('offset','G','10');
        if(empty($news_id)) {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $postInfo = $this->load('bbs')->getPostInfo($news_id);
        if($postInfo['is_show'] == 0){
            $_ENV['error']->getErrors(801);
            exit;
        }
        if($comment_id > 0 && $this->vn >= 2500)
        {
            $hotComments = array();
        }
        else
        {
            $hotComments = $this->load('bbs')->getHotComment($news_id);
        }
        //统计 点击数据
        $this->load('news')->makeNewsClickStat($news_id);
        
        $allComments = $this->load('bbs')->getCommentList($news_id, $comment_id, $direc, $offset);
        $totalComments = intval($this->redis->hget(REDIS_PREFIX. 'PostInfo_'. $news_id, 'total_comment'));
        $this->result['result'] = compact('hotComments', 'allComments', 'total_comment');
    }
    /**
     * 发帖
    */
    public function dopost()
    {
        $boardId = common::request('boardId', 'P');
        $title = common::request('title', 'P', '');
        $content = common::request('content', 'P');
        $tag = common::request('tag', 'P');
        $gid = common::request('gid','P',0);
        $synchronize = common::request('synchronize','P','');
        $lat = common::request('lat',"P",0);
        $lng = common::request('lng',"P",0);
        $uid = $this->uid;
        $is_show = 1;
        if(!$uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }

        // 防止重复帖子
        $this->cacheRedis = $this->in_redis('cacheRedis');
        $uniqueKey = REDIS_PREFIX . 'Post_Unique_' . md5(json_encode([$uid, $title, $content, $_FILES]));

        if($this->cacheRedis->incrby($uniqueKey, 1) > 1)
        {
            $_ENV['error']->getErrors(308);
            exit;
        }
        // 设置自动超时，10分钟内不能提交相同内容
        $this->cacheRedis->expire($uniqueKey, 600);

        if(empty($boardId))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        // 检查 title
        if($this->vn < 2500 || ($this->vn >= 2500 && ($boardId != 'showtime' && $boardId != 'groupNews')))
        {
          if(empty($title))
          {
              $_ENV['error']->getErrors(101);
              exit;
          }
        }
        if($this->load('badwords')->check($title) || $this->load('badwords')->check($content))
        {
            //$_ENV['error']->getErrors(605);
            //exit;
            $userInfo = $this->load('client')->getUserInfo($uid);
            if(!$userInfo['phone']){
                $_ENV['error']->getErrors(402);
                exit;
            }
            $is_show = 0;
        }

        // 检查 content
        if($this->vn < 2500 || ($this->vn >= 2500 && $boardId != 'hotans'))
        {
          // 通过客户端发的帖子，过滤内容中除了『p、br』的所有标签
          $content && $content = strip_tags($content, '<p><br><at>');

          if(empty($content))
          {
              $_ENV['error']->getErrors(101);
              exit;
          }
        }

        if($this->vn >= 2700){
            if(empty($gid) && $boardId == "groupNews"){
                $_ENV['error']->getErrors(101);
                exit;
            }
        }
        $bbs_mod = $this->load('bbs');
        $short_desc = $bbs_mod->cutString($content, 100);
        $imgContent = $bbs_mod->getPostImage('images_');
        $atData = $bbs_mod->filterAt($content);
        $content = $atData['string'];
        $atUids = $atData['uids'];
        extract($imgContent);
        $news_id = $bbs_mod->addPost($boardId, $uid, $title, $content,$short_desc,$sourceImg, $thumbImg, $tag, $validImg, 0,$gid,$synchronize,$lat,$lng,0,$is_show);

        //发消息
        //发push
        if($atUids){
            foreach($atUids as $toUserId){
                $bbs_mod->sendMessage($uid,$toUserId,$news_id,null,'','N11');
            }
            $data = array('title'   => "有一条跑友秀中提到了你，去看看吧。",
                          'text'    => '',
                          'body'    => "有一条跑友秀中提到了你，去看看吧。",
                          'url'    => 'joggers://news/'.$news_id,
                          'sound' => "1"
                        );
            $this->load('push')->pushNoticeByUid($toUserId,$data);
        }
        
        //发勋章
        if($this->afterVersion('2.9.1','2.9.1')){
            $medal_mod = $this->load('medal');
            $medal_id = $medal_mod->addMedalByTag($uid,'post');
            if($medal_id){
                $this->result['result']['medal_id'] = [$medal_id];
            }
        }
        
        $this->result['result']['is_success'] = $news_id ? 1: 0;

    }
    /**
     * 发表帖子评论
    */
    public function dopublishComment()
    {
        if(!$this->uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $ip = common::getip(true);
        $news_id = common::request('news_id', 'P');
        $comment_content = common::request('comment_content', 'P');
        $father_id = common::request('father_id', 'P');
        $postInfo = $this->load('bbs')->getPostInfo($news_id);
        if($postInfo['is_show'] <= 0){
            $_ENV['error']->getErrors(801);
            exit;
        }
        if(!$news_id || empty($comment_content))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        if ($this->redis->exists(REDIS_PREFIX.'ForbidComment'.$ip))
        {
          $_ENV['error']->getErrors(208);
          exit();
        }
        if($this->load('badwords')->check($comment_content))
        {
            $_ENV['error']->getErrors(605);
            exit;
        }
        $preTime = $this->redis->get(REDIS_PREFIX.'PreComment'.$ip);
        $time = time();
        if($time - ($preTime + 0.5) < 0 ){
            $this->redis->set(REDIS_PREFIX.'ForbidComment'.$ip,1,10);
            $_ENV['error']->getErrors(208);
            exit;
        }
        $comment_img = $this->load('qiniu')->uploadQiNiuImg('comment_img', 'PC/'.md5($news_id.'/'.time().common::getHashStr(6)));
        $this->redis->set(REDIS_PREFIX.'PreComment'.$ip,time(),10);
        
        $this->load('bbs')->publishComment($news_id, $comment_content, $this->uid,$father_id, $comment_img);
    }
    /**
     * 喜欢某帖子
     * @return int is_success
     */
    public function dolikePost()
    {
        if(!$this->uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $news_id = common::request('news_id', 'G');
        $uid = $this->uid;
        $postInfo = $this->load('bbs')->getPostInfo($news_id);
        if(empty($postInfo) || $postInfo['is_show'] != 1 || !$uid)
        {
            $_ENV['error']->getErrors(101);
            exit();
        }
        if($this->vn < 2400) // 收藏
        {
            if($this->redis->ZSCORE(REDIS_PREFIX.'CollectPostList_'.$uid, $news_id) == false)
            {
                if($_ENV['bbs']->collectPost($news_id, $uid))
                {
                    $this->redis->zadd(REDIS_PREFIX.'CollectPostList_'.$uid, time(), $news_id);
                }
            }
        }
        if($this->redis->ZSCORE(REDIS_PREFIX.'PostLikeList_'.$news_id, $uid) !==false)
        {
            $is_success = 1;
        }
        else
        {
            $record_id = $_ENV['bbs']->likePost($news_id, $uid);
            $is_success = $record_id ? 1 : 0;
            if($is_success)
            {
                $this->redis->zadd(REDIS_PREFIX.'PostLikeList_'.$news_id, time(), $uid);
                $this->redis->zadd(REDIS_PREFIX.'MyLovePList_'.$uid, $record_id, $news_id);
                //$this->db->update("update r_news set loves = loves+1 where news_id = '{$news_id}'");
                $_ENV['bbs']->update(['loves'=>'?+1','news_id'=>$news_id]);
                $this->redis->HINCRBY(REDIS_PREFIX. 'NewsInfo_'.$news_id, 'loves', 1);
                if($uid != $postInfo['uid'])
                {
                    $_ENV['bbs']->sendMessage($uid, $postInfo['uid'],$news_id,'','',"N1");
                    $_ENV['bbs']->addUnReadMsg($postInfo['uid'], array('msgType'=>4, 'sponsor'=>array('uid'=>$uid,'comment_id'=>'0'), 'quote'=>array('news_id'=>$news_id,'comment_id'=>'0')));
                }
            }
        }
        
        //发勋章
        if($this->afterVersion('2.9.1','2.9.1')){
            $medal_mod = $this->load('medal');
            $medal_id = $medal_mod->addMedalByTag($uid,'likePost');
            if($medal_id){
                $this->result['result']['medal_id'] = [$medal_id];
            }
        }

        $this->result['result']['is_success'] = $is_success;
        $this->result['result']['news_id'] = $news_id;
    }
    /**
     * 取消喜欢某帖子
     * @return int is_success
     */
    public function dounLikePost()
    {
        $uid = $this->uid;
        if(!$uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $news_id = common::request('news_id', 'G');
        $postInfo = $this->load('bbs')->getPostInfo($news_id);
        if(empty($postInfo) || $postInfo['is_show'] != 1 || !$uid)
        {
            $is_success = 1;
        }
        else
        {
            if($this->vn < 2400) // 取消收藏
            {
                if($this->redis->ZSCORE(REDIS_PREFIX.'CollectPostList_'.$uid, $news_id) !== false)
                {
                    if($_ENV['bbs']->discollectPost($news_id, $uid))
                    {
                        $this->redis->ZREM(REDIS_PREFIX.'CollectPostList_'.$uid, time(), $news_id);
                    }
                }
            }
            
            if($this->redis->ZSCORE(REDIS_PREFIX.'PostLikeList_'.$news_id, $uid) === false)
            {
                $is_success = 1;
            }
            else
            {
                $is_success = $_ENV['bbs']->unlikePost($news_id, $uid);
                if($is_success)
                {
                    $this->db->update("update r_news set loves = loves-1 where news_id = '{$news_id}'");
                    //$_ENV['bbs']->update(['loves'=>'?-1','news_id'=>$news_id]);
                    $this->redis->HINCRBY(REDIS_PREFIX. 'NewsInfo_'.$news_id, 'loves', -1);
                }
            }
        }

        if($is_success)
        {
            $this->redis->zrem(REDIS_PREFIX.'PostLikeList_'.$news_id, $uid);
            $this->redis->zrem(REDIS_PREFIX.'MyLovePList_'.$uid, $news_id);
        }

        $this->result['result']['is_success'] = $is_success;
        $this->result['result']['news_id'] = $news_id;
    }
    /**
     * 喜欢某帖子的用户列表
     * @return int is_success
     */
    public function dopostLikeList()
    {
        $news_id = common::request('news_id', 'G');
        $point = common::request('uid', 'G');
        $direc = common::request('direc', 'G', 'next');
        $offset = common::request('offset', 'G', 20);
        if($news_id < 1)
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $this->result['result']['num'] = $this->redis->zcard(REDIS_PREFIX.'PostLikeList_'.$news_id);
        $this->result['result']['list'] = $this->load('bbs')->getPostLikeList($news_id, $point, $direc, $offset);
    }
    /**
     * 收藏某帖子
     * @return int is_success
     */
    public function docollectPost()
    {
        if(!$this->uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }

        $news_id = common::request('news_id', 'G');
        $uid = $this->uid;
        $postInfo = $this->load('bbs')->getPostInfo($news_id);
        if(empty($postInfo) || $postInfo['is_show'] != 1 || !$uid)
        {
            $_ENV['error']->getErrors(101);
            exit();
        }
        if($this->redis->ZSCORE(REDIS_PREFIX.'CollectPostList_'.$uid, $news_id) !==false)
        {
            $is_success = 1;
        }
        else
        {
            $record_id = $_ENV['bbs']->collectPost($news_id, $uid);
            $is_success = $record_id ? 1 : 0;
            if($is_success)
            {
                $this->redis->zadd(REDIS_PREFIX.'CollectPostList_'.$uid, time(), $news_id);
            }
        }

        $this->result['result']['is_success'] = $is_success;
        $this->result['result']['news_id'] = $news_id;
    }
    /**
     * 取消收藏某帖子
     * @return int is_success
     */
    public function dodiscollectPost()
    {
        $uid = $this->uid;
        if(!$uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $news_id = common::request('news_id', 'G');
        $postInfo = $this->load('bbs')->getPostInfo($news_id);
        if(empty($postInfo) || $postInfo['is_show'] != 1 || !$uid)
        {
            $is_success = 1;
        }
        else
        {
            if($this->redis->ZRANK(REDIS_PREFIX.'CollectPostList_'.$uid, $news_id) === false)
            {
                $is_success = 1;
            }
            else
            {
                $is_success = $_ENV['bbs']->discollectPost($news_id, $uid);
                if($is_success)
                {
                    $this->redis->ZREM(REDIS_PREFIX.'CollectPostList_'.$uid, $news_id);
                }
            }
        }

        $this->result['result']['is_success'] = $is_success;
        $this->result['result']['news_id'] = $news_id;
    }
    /**
     * 我收藏的帖子列表
     * @return array
     */
    public function docollectList()
    {
        $uid = $this->uid;
        if(!$uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $news_id = common::request('news_id', 'G');
        $direc = common::request('direc', 'G');

        $this->result['result'] = $this->load('bbs')->getCollectionList($uid, $news_id, $direc);
    }
    /**
     * 我的帖子列表
     * @return int is_success
     */
    public function domyPost()
    {
        $news_id = common::request('news_id', 'G');
        $direc = common::request('direc', 'G', 'next');
        $offset = common::request('offset', 'G', 20);
        $news_uid = common::request('news_uid','G');
        empty($news_uid) && $news_uid = $this->uid;
        $uid = $this->uid;
        if(!$news_uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $bbs_mod = $this->load('bbs');
        $postIds = $bbs_mod->getUserPostList('', $news_uid, $news_id, $direc, $offset+10);
        
        $postList = [];
        if(!empty($postIds)){
            $client_mod = $this->load('client');
            $user_mod = $this->load('user');
            foreach($postIds as $postId){
                
                if(count($postList) >= 10)
                {
                    break;
                }
                $postInfo = $bbs_mod->getPostInfo($postId);
                if(!empty($postInfo) && $postInfo['is_show'])
                {
                    $userInfo = $client_mod->getUserInfo($postInfo['uid']);

                    $tmpPost = array('news_id'     => $postInfo['news_id'],
                                      'type'       => isset($postInfo['type']) ? $postInfo['type'] : $bbs_mod->getPostType($postInfo['content']),
                                      'title'       => $postInfo['title'],
                                      'short_desc'  => $postInfo['short_desc'],
                                      'tag'  => $postInfo['tag'],
                                      'thumb_img'   => $postInfo['thumb_img'],
                                      'middle_img'   => $bbs_mod->getPostMidImg($postInfo['sourceImg']),
                                      'source_img'   => $postInfo['sourceImg'],
                                      'uid'         => $postInfo['uid'],
                                      'nickname'  => isset($userInfo['nickname']) ? $userInfo['nickname'] : '',
                                      'header'  => isset($userInfo['header']) ? $userInfo['header'] : '',
                                      'attendance'  => $user_mod->getAttendance($postInfo['uid']),
                                      'isLove' => $this->redis->ZSCORE(REDIS_PREFIX.'PostLikeList_'.$postId, $uid) === false ? 0 : 1,
                                      'isCollect' => $this->redis->ZSCORE(REDIS_PREFIX.'CollectPostList_'.$uid, $postId) === false ? 0 : 1,
                                      'add_time'    => $postInfo['add_time'],
                                      'loves'       => $postInfo['loves'],
                                      'total_comment'  => $postInfo['total_comment'],
                                      'imginfo'  =>  isset($postInfo['imginfo']) ? json_decode($postInfo['imginfo'],true) : array(),
                                      'board' => $postInfo['board'],
                        );
                    
                    if($this->vn >= 2820){
                        $tmpPost['energy'] = $postInfo['energy'];
                        $tmpPost['energy_pay_count'] = $postInfo['energy_pay_count'];
                        $tmpPost['is_paid_energy'] = $_ENV['client']->isPostPaidBy($postInfo['news_id'],$this->uid);
                    }
                    // 图文混排类型的帖子，图片只显示一张
                    if($tmpPost['type'] == 1)
                    {
                       $tmpPost['thumb_img'] = !empty($tmpPost['thumb_img']) ? array($tmpPost['thumb_img'][0]) : array();
                       $tmpPost['middle_img'] = !empty($tmpPost['middle_img']) ? array($tmpPost['middle_img'][0]) : array();
                       $tmpPost['source_img'] = !empty($tmpPost['source_img']) ? array($tmpPost['source_img'][0]) : array();
                       $tmpPost['imginfo'] = !empty($tmpPost['imginfo']) ? array($tmpPost['imginfo'][0]) : array();
                    }
                    // 列表中帖子显示的文字内容
                    $tmpPost['content'] = $tmpPost['type'] ? $postInfo['short_desc'] : $postInfo['content'];
                    // 喜欢帖子的用户列表
                    $loveList = $bbs_mod->getPostLikeList($postId,0,'next',8);
                    if($loveList)
                    {
                        foreach ($loveList as $key => $v)
                        {
                            unset($loveList[$key]['level'],$loveList[$key]['nickname'],$loveList[$key]['gender'],$loveList[$key]['total_rate'],$loveList[$key]['total_mileage']);
                        }
                    }
                    $tmpPost['loveList'] = empty($loveList) ? array() : $loveList;
                    // 评论列表
                    $tmpPost['comments'] = $bbs_mod->getCommentsNoImg($postId);
                    if(!$tmpPost['title'])
                    {
                        $tmpPost['title'] = $bbs_mod->cutString($tmpPost['content'], 200);
                    }
                    $postList[] = $tmpPost;
                }
            }
            
        }
        $this->result['result'] = $postList;
    }
    /**
     * 我的回复列表
     */
    public function domyCommentList()
    {
        $uid = $this->uid;
        if(!$uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $comment_id = common::request('comment_id', 'G');
        $direc = common::request('direc', 'G', 'next');
        $offset = common::request('offset', 'G', 10);

        $this->result['result'] = $this->load('bbs')->getMyCommentPost($uid, $comment_id, $direc, $offset);
    }
    /**
     * 我喜欢的帖子列表
     */
    public function domyLikePost()
    {
        $uid = $this->uid;
        $point = common::request('collection_id', 'G');
        $direc = common::request('direc', 'G', 'next');
        $offset = common::request('offset', 'G', 10);
        if(!$uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $this->result['result'] = $this->load('bbs')->getMyLikePost($uid, $point, $direc, $offset);
    }
    /**
     *  点亮评论
     * @return int is_success
     */
    public function doupComment()
    {
        $comment_id = common::request('comment_id','G');
        $uid = $this->uid;
        if(!$comment_id)
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        if(!$uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $this->result['result']['is_success'] = $this->load('bbs')->upComment($uid, $comment_id);
    }
    /**
     * 删除我的帖子
     * @return int is_success
     */
    public function dodelMyPost()
    {
        $news_id = common::request('news_id','G');
        $uid = $this->uid;
        if(!$news_id )
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        if(!$uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        //删除群空间帖子
        $newsInfo = $this->load('group')->getNews($news_id);
        foreach($newsInfo as $info){
            $this->redis->zrem(REDIS_PREFIX.'GroupNews_'.$info['gid'],$news_id);
            $this->db->query("update r_group_news set is_show=0 where news_id=$news_id and uid=$uid");
        }
        $this->result['result']['is_success'] = $this->load('bbs')->hidePost($uid, $news_id);
    }
    /**
     * 删除我的评论
     * @return int is_success
     */
    public function dodelMyComment()
    {
        $comment_id = common::request('comment_id','G');
        $uid = $this->uid;
        if(!$comment_id )
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        if(!$uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $this->result['result']['is_success'] = $this->load('bbs')->hideComment($uid, $comment_id);
    }
    /**
     * 未读通知信息数量
     * @return int count
     */
    public function dounReadMsgCount()
    {
        $uid = $this->uid;
        if(!$uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $this->result['result']['count'] = $this->load('bbs')->getunReadMsg($uid, true);
    }
    /**
     * 未读通知信息列表
     * @return array
     */
    public function dounReadMsgList()
    {
        $uid = $this->uid;
        if(!$uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        $this->result['result'] = $this->load('bbs')->getunReadMsg($uid);
        $this->redis->del(REDIS_PREFIX.'unReadBbsMsg_'.$uid);
    }
    
    /*
     * 每日问答，获取随机指定文章
     */
    public function dorandAns(){
        $since_id = common::request('since_id','G',0);
        $limit = common::request('limit','G',1);
        $bbs_mod = $this->load('bbs');
        $qa_ids = $bbs_mod->getRandAns($limit,$since_id);
        $res = [];
        foreach($qa_ids as $news_id){
            $post = $bbs_mod->getPostInfo($news_id);
            $post['content'] = strip_tags($post['content']);
            $post['content'] = str_replace(["\r","\t","\n"],'',$post['content']);
            $res[] = $post;
        }
        $this->result['result'] = $res;
    }
    
    /*
     * 推荐话题
     */
    public function dotopicList(){
        $topics = array_values($this->load('topic')->getFeaturedTopics());
        $this->result['result'] = $topics;
    }
    
    /*
     * 通知浏览器登录并上传帖子草稿
     */
    public function dopostDraft(){
        $code = common::request('code','P');
        $boardId = common::request('boardId', 'P');
        $title = common::request('title', 'P', '');
        $content = common::request('content', 'P');
        $tag = common::request('tag', 'P');
        $gid = common::request('gid','P',0);
        $synchronize = common::request('synchronize','P','');
        $lat = common::request('lat',"P",0);
        $lng = common::request('lng',"P",0);
        $uid = $this->uid;
        
        if(!$uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        if(empty($boardId))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        
        if($this->vn >= 2700){
            if(empty($gid) && $boardId == "groupNews"){
                $_ENV['error']->getErrors(101);
                exit;
            }
        }
        
        if(empty($code)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $app_key = $this->config['app_key'];
        $code = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128,$app_key,base64_decode($code),MCRYPT_MODE_ECB));
        if(!common::is_json($code)){
            $_ENV['error']->getErrors(101);
            exit;
        }
        extract(json_decode($code,true));//auth_key,auth_value1
        $redisKey = REDIS_PREFIX.'postDraft_'.$auth_key;
        if($this->redis->exist($redisKey) || $this->redis->hget($redisKey,'auth_value1') != $auth_value1){
            $_ENV['error']->getErrors(802);
            exit;
        }
        
        //更新一下避免过期
        $this->redis->hset($redisKey,'news_draft_id',0);
        
        $vn = $this->vn;
        $type = 1;
        $news_draft_id = $this->load('bbs')->addDraft(compact('boardId','title','content','tag','gid','synchronize','lat','lng','uid','vn','type'));
        if($news_draft_id){
            $this->redis->hset($redisKey,'news_draft_id',$news_draft_id);
            $token = common::getHashStr(12);//base64_encode(mcrypt_decrypt(MCRYPT_RIJNDAEL_128,$app_key,$uid,MCRYPT_MODE_ECB));
            $this->redis->hset($redisKey,$token,$uid);
            $this->redis->expire($redisKey,172800); //两天
            $this->in_redis('pubsubRedis');
            $this->pubsubRedis->publish(REDIS_PREFIX.'postDraft_notify','{"auth_key":"'.$auth_key.'","token":"'.$token.'"}');
            $this->result['result'] = ['is_success'=>1];
        }else{
            $this->result['result'] = ['is_success'=>0];
        }
    }
    
    /*
     * 获得话题详情
     */
    public function dotopicInfo(){
        $topic = trim(common::request('topic','G'));
        $topic && $topic = urldecode($topic);
        if(!$topic){
            $_ENV['error']->getErrors(101);
            exit;
        }
        $topic_mod = $this->load('topic');
        $topicInfo = $topic_mod->getTopicInfo($topic);
        if(!$topicInfo){
            $topicInfo = [
                'name'=>$topic
            ];
        }else{
            $topic_mod->countStatistics($topic);
        }
        $topicInfo['pv'] = 0;
        $this->result['result'] = $topicInfo ? $topicInfo : ['success'=>0];
    }
    
    /*
     * 上传单张图片
     */
    public function douploadFile(){
        $bbs_mod = $this->load('bbs');
        $res = $bbs_mod->getPostImage('images_',1,0);
        extract($res);//'sourceImg', 'thumbImg', 'imgHtml', 'validImg'
        
        if(isset($sourceImg[0])){
            $result = [
                "file_path"=> $sourceImg[0]
            ];
        }else{
            $result = [
                'success'=>0
            ];
        }
        $this->result['result'] = $result;
        
        $this->breakCall();
        if(isset($sourceImg[0])){
            $bbs_mod->insertTempImg($sourceImg[0]);
        }
    }

    /**
     * 新增用户投诉
    */
    public function doaddComplaints()
    {
        if(!$this->uid)
        {
            $_ENV['error']->getErrors(201);
            exit;
        }
        //$ip = common::getip(true);
        $news_id = common::request('news_id', 'P');   //帖子id
        $reason = common::request('reason', 'P');  //投诉原因
        if(!is_numeric($news_id) || !is_numeric($reason))
        {
            $_ENV['error']->getErrors(101);
            exit;
        }
        $uid = $this->uid;    //用户id 

        //查询是否重复投诉
        $ComplaintsInfo = $this->load('bbs')->getComplaints($news_id, $uid, $reason);
        if($ComplaintsInfo)
        {
            $_ENV['error']->getErrors(308);
            exit;
        }
        //$this->redis->set(REDIS_PREFIX.'PreComment'.$ip,time(),10);

        $params = compact('uid', 'news_id', 'reason');
        //新增投诉信息
        $data = $this->load('bbs')->addComplaints($params);
        //查询投诉数量
        $countInfo = $this->load('bbs')->getComplaintsCount($news_id, $reason);
        
        if($countInfo)
        {
            //更新帖子投诉数量
            $countData = $this->load('bbs')->updateComplaintsCount($news_id);
        }
        else
        {
            $countParams = array
            (
                'news_id' => $news_id,
                'reason' => $reason,
                'count' => 1
            );
            //新增帖子投诉数量
            $countData = $this->load('bbs')->addComplaintsCount($countParams);
        }
        
        $this->result['result']['news_id'] = $news_id;
        $this->result['result']['c_id'] = $data;
    }

}